<?php
declare (strict_types=1);

namespace app\model\home;

use app\common\model\BaseModel;
use \libs\Log;
use think\Db;

class Cp extends BaseModel
{
    protected $pk = 'id';

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 获得列表
     * @param bool $where
     * @param string $fields
     * @param string|null $order
     * @param int $page
     * @param int $pageSize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($where = true, $fields = '*', $order = null, $page = 1, $pageSize = 20)
    {
        $data = [
            'count' => 0,
            'list' => []
        ];
        $count = $this->where($where)->count();
        if (empty($count)) {
            return $this->returnRight($data);
        }
        $order = is_null($order) ? $this->pk . ' desc' : $order;
        $list = $this->where($where)
            ->field($fields)
            ->order($order)
            ->page($page, $pageSize)
            ->select();
        $data['count'] = $count;
        $data['list'] = $list;
        return $this->returnRight($data);
    }

    public function getOne($data, $fields = '*')
    {
        $info = self::where($this->pk, $data['id'])->field($fields)->find();
        return $info;
    }

    /**
     * 添加
     * @param array $data
     * @return array
     */
    public function add(array $data)
    {
        Log::info('add in parameter', $data);
        $assistant_id = Assistant::where('member_id', $data['member_id'])->value('id');
        if (empty($assistant_id)) {
            return $this->returnError(33);
        }
        $onging = $this->where('recipient_id|initiator_id', '=', $assistant_id)
            ->where('status', '=', 0)
            ->count();
        if (!empty($onging)) {
            return $this->returnError(35);
        }
        $count = $this->where('recipient_id|initiator_id', '=', $assistant_id)
            ->where('status', '=', 1)
            ->where('delete_time', '=', 0)
            ->count();
        if (!empty($count)) {
            return $this->returnError(34);
        }
        $data['initiator_id'] = $assistant_id;
        // 6.0自动过滤非数据表字段
        self::create($data);
        return $this->returnSucc('调用成功');
    }

    /**
     * 更新
     * @param array $data
     * @return array
     */
    public function edit(array $data, $field)
    {
        $where['id'] = $data['id'];
        $fieldArr = explode(',', $field);
        $this->allowField($fieldArr)
            ->update($data, $where);
        return $this->returnSucc('编辑成功');
    }


    /**
     * 状态更新：启用，禁用，软删除
     * @param array $data
     * @param int $status
     * @return array
     */
    public function changeStatus(array $data, int $status)
    {
        $where['id'] = $data['id'];
        $data['status'] = $status;
        $res = self::update($data, $where);
        if ($res) {
            return $this->returnSucc();
        } else {
            return $this->returnError(1);
        }
    }

    /**
     * @param $data
     * @return array
     * 删除
     */
    public function del($data)
    {
        $res = self::update(['delete_time' => time()], ['id' => $data['id']]);
        if ($res) {
            return $this->returnSucc();
        } else {
            return $this->returnError(1);
        }
    }

    /**
     * 获取未绑定助教信息
     */
    public function get_assistant_list($data)
    {
        $list = \think\facade\Db::name('assistant')->alias('a')
            ->join('cp p', 'p.recipient_id=a.id or p.initiator_id=a.id', 'left')
            ->field('a.id,a.nickname,a.avatar,p.status,a.member_id,p.delete_time')
            ->where('a.member_id', '<>', 0)
            ->where('a.member_id', '<>', $data['member_id'])
            ->select();
        $listArr = [];
        foreach ($list->toArray() as $item) {
            if ($item['status'] != 1 && empty($item['delete_time']) && $item['status'] != 2) {
                $listArr[] = $item;
            }
        }
        return $this->returnRight($listArr);
    }

    /**
     * @param $data
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 我的CP
     */
    public function my_cp($data)
    {
        $assistant_id = Assistant::where('member_id', $data['member_id'])->value('id');
        if (empty($assistant_id)) {
            return $this->returnError(33);
        }
        $cp = $this->where('recipient_id|initiator_id', '=', $assistant_id)
            ->where('delete_time', 0)
            ->where('status', '<>', 2)
            ->find() ?: [];
        $info = [];
        if (!empty($cp)) {
            if ($cp['initiator_id'] == $assistant_id) {
                $info = Assistant::where('id', $cp['recipient_id'])->field('id,nickname,store_id,avatar,profile,cover,status')->find();
            } else {
                $info = Assistant::where('id', $cp['initiator_id'])->field('id,nickname,store_id,avatar,profile,cover,status')->find();
            }
            $info['cp_status'] = $cp['status'];
        }
        return $this->returnRight($info);
    }

    /**
     * @param $data
     * @return array
     * 绑定
     */
    public function unbind($data)
    {
        $assistant_id = Assistant::where('member_id', $data['member_id'])->value('id');
        if (empty($assistant_id)) {
            return $this->returnError(33);
        }
        $res = $this->where('recipient_id|initiator_id', '=', $assistant_id)
            ->save(['delete_time' => time()]);
        if ($res) {
            return $this->returnSucc();
        } else {
            return $this->returnError(-1);
        }
    }
}
